package xue_sheng;

import JDBC.jdbc_user;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class student extends JFrame {
    public student(){
        setTitle("学生信息管理");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 600);
        setLocation(500,200);
        setLayout(new BorderLayout());

        //创建按钮与输入框
        JPanel inputPanel = new JPanel(new GridLayout(2,4,30,10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 四周留白10px

        JButton addButton = new JButton("添加");
        inputPanel.add(addButton);
        JButton updateButton = new JButton("修改");
        inputPanel.add(updateButton);
        JLabel label = new JLabel((String) null);
        inputPanel.add(label);
        JButton suanButton= new JButton("刷新");
        inputPanel.add(suanButton);

        JButton createButton = new JButton("查询");
        inputPanel.add(createButton);
        JButton delectButton = new JButton("删除");
        inputPanel.add(delectButton);
        JLabel label_1 = new JLabel("ID：");
        inputPanel.add(label_1);
        JTextField idInput = new JTextField(10);
        inputPanel.add(idInput);

        add(inputPanel, BorderLayout.NORTH);

        //创建表格
        String[] columns = {"ID", "姓名", "性别","年龄","专业","年级","诚信度"};
        DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
        JTable JTable = new JTable(tableModel);
        JScrollPane jScrollPane = new JScrollPane(JTable);
        add(jScrollPane, BorderLayout.CENTER);

        addButton.addActionListener(e->new AddStudent());
        updateButton.addActionListener(e->new UpdateStudent());
        delectButton.addActionListener(e-> Delect(tableModel, idInput.getText()));
        suanButton.addActionListener(e-> shuaxin(tableModel));
        createButton.addActionListener(e-> create(tableModel, idInput.getText()));

        loadDate(tableModel);
        setVisible(true);
    }

    //刷新
    private void shuaxin(DefaultTableModel tableModel) {
        tableModel.setRowCount(0);
        loadDate(tableModel);
    }

    //显示
    private void loadDate(DefaultTableModel tableModel) {
        try{
            Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("select * from STUDENT ORDER BY STU_ID ASC");
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                Object[] row = {
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7)
                };
                tableModel.addRow(row);
            }
            pstm.close();
            conn.close();
        } catch (SQLException e){
            showError("加载失败："+e.getMessage());
        }
    }

    //查询
    private void create(DefaultTableModel tableModel, String text) {
        tableModel.setRowCount(0);
        try{
            int i= Integer.parseInt(text);
            Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("select * from STUDENT where STU_ID=?");
            pstm.setInt(1,i);//写入
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                Object[] row = {
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7)
                };
                tableModel.addRow(row);
            }
            pstm.close();
            conn.close();
        } catch (SQLException e){
            showError("查询失败："+e.getMessage());
        }
    }

    //删除
    private void Delect(DefaultTableModel tableModel, String text) {
        tableModel.setRowCount(0);
        int i= Integer.parseInt(text);
        try{Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("delete from STUDENT where STU_ID=?");
            //写入
            pstm.setInt(1,i);
            int j = pstm.executeUpdate();
            loadDate(tableModel);
            pstm.close();
            conn.close();
        }catch (SQLException e){
            showError("删除失败"+e.getMessage());
        }
    }

    //弹窗
    private void showError(String s) {
        JOptionPane.showMessageDialog(this,s,"错误",JOptionPane.ERROR_MESSAGE);
    }
}
